package com.java2007.controller.front;

import com.alibaba.druid.util.StringUtils;
import com.java2007.controller.BaseServlet;
import com.java2007.entity.PageBean;
import com.java2007.factory.BeanFactory;
import com.java2007.pojo.Food;
import com.java2007.pojo.FoodType;
import com.java2007.pojo.Table;
import com.java2007.service.FoodService;
import com.java2007.service.FoodTypeService;
import com.java2007.service.TableService;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 前端页面处理层
 */
@WebServlet("/front")
public class FrontController extends BaseServlet {
    private TableService tableService= (TableService) BeanFactory.getBean("com.java2007.service.impl.TableServiceImpl");
    private FoodService foodService = (FoodService) BeanFactory.getBean("com.java2007.service.impl.FoodServiceImpl");
    private FoodTypeService foodTypeService = (FoodTypeService) BeanFactory.getBean("com.java2007.service.impl.FoodTypeServiceImpl");
    /**
     * 查询所有没有预约的餐桌
     * @param request
     * @param response
     * @return
     */
    public String findAll(HttpServletRequest request, HttpServletResponse response) throws Exception{
            List<Table> fTables = tableService.queryAlliDle();
            request.setAttribute("fTables",fTables);
            //存一次typeName，到时候方便直接取，就不用在查了
            List<FoodType> foodType = (List<FoodType>) request.getSession().getAttribute("foodType");
                if (foodType==null || foodType.size()==0){
                    foodType=foodTypeService.findAll();
                    request.getSession().setAttribute("foodType",foodType);
                }
            return "forward:front/index.jsp";
    }

    /**
     * 详情页菜品展示
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findFoodById(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String foodIdStr = request.getParameter("foodId");
        Integer foodId= Integer.valueOf(foodIdStr);
        Food food = foodService.findByFoodId(foodId);
        request.setAttribute("food",food);
        return "forward:/front/detail/caixiangxi.jsp";
    }


    /**
     * 分页查询菜品
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findFoodByPage(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //将餐桌id存到session中，结账的时候用
        String tableId = request.getParameter("tableId");
        if (tableId!=null || !StringUtils.isEmpty(tableId)){
            request.getSession().setAttribute("tableId",tableId);
        }
        //将以下数据进行包装，方便传参
        Food food= new Food();
        //获取菜类别名
        String typeNameStr = request.getParameter("typeName");
        if (!StringUtils.isEmpty(typeNameStr)){
            food.setTypeName(typeNameStr.trim());
        }
        //获取菜品名
        String foodNameStr = request.getParameter("foodName");
        if (!StringUtils.isEmpty(foodNameStr)){
            food.setFoodName(foodNameStr.trim());
        }
        //获取当前页码
        String pageNoStr = request.getParameter("pageNo");
        if (StringUtils.isEmpty(pageNoStr)){
            pageNoStr="1";
        }
        //通过当前页，页大小，分页查询结果
        Integer pageNo= Integer.valueOf(pageNoStr);
        PageBean<Food> pageBean=foodService.findByPage(pageNo,PageBean.SIZE,food);
        request.setAttribute("pageBean",pageBean);
        request.setAttribute("foodName",foodNameStr);
        return "forward:/front/detail/caidan.jsp";
    }
}
